package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.cb.*;
import com.sdry.model.llm.LlmPrepaidExpenses;
import com.sdry.model.lz.Warehouse;
import com.sdry.service.cb.AmoebaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;


/**
 * 阿米巴结算
 * @author cb
 * 2019年9月28日14:11:54
 */
@Controller
@RequestMapping("amoeba")
public class AmoebaController extends BaseController {


    @Autowired
    private AmoebaService amoebaService;

    @RequestMapping()
    public String index(Model model) {
        //内部收入,先添加一条空数据，向页面给一个UUID

        //仓库
        List<Warehouse> warehouseList = iDicMapper.getWarehouseList();
        model.addAttribute("warehouseList",warehouseList);
        return "/cb/amoeba";
    }

    @RequestMapping("listPartA")
    @ResponseBody
    public TableDataInfo listPartA(
            Long stockId,
            String date
    ) {
        List<AmoebaEntity> amoebaEntities = amoebaService.listPartA(stockId,date);
       return getDataTable(amoebaEntities,0);
    }

    @RequestMapping("getList")
    @ResponseBody
    public CostUnit getList(
            Long stockId,
            String date
    ) throws ParseException {

        CostUnit costUnit = amoebaService.getList(stockId,date);
        return costUnit;
    }


    @RequestMapping("sharePage")
    public String sharePage(Model model) {
        //仓库
        List<Warehouse> warehouseList = iDicMapper.getWarehouseList();
        model.addAttribute("warehouseList",warehouseList);
        return "cb/share";
    }

    @RequestMapping("getShareList")
    @ResponseBody
    public TableDataInfo getShareList(
            @RequestParam("page") Integer page ,
            @RequestParam("pageSize") Integer pageSize,
            Long stockId,
            String date
    ) {
        PageHelper.startPage(page,pageSize);
        List<ShareEntity> list = amoebaService.getShareList(date,stockId);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
    }


    @RequestMapping("delShare")
    @ResponseBody
    public Integer del (@RequestParam("idArr[]") String[] idArr) {
        return amoebaService.deleteShare(idArr);
    }

    /**
     * 摊销
     * @param page
     * @param pageSize
     * @param warehouseId
     * @param dateOfOccurrence
     * @return
     */
    @RequestMapping("getAmortizationList")
    @ResponseBody
    public TableDataInfo getAmortizationList(
            @RequestParam("page") Integer page ,
            @RequestParam("pageSize") Integer pageSize,
            Long warehouseId,
            String dateOfOccurrence
    ) {
        PageHelper.startPage(page,pageSize);
        List<LlmPrepaidExpenses> list =amoebaService.getAmortizationList(warehouseId,dateOfOccurrence);
        PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
    }

    /**
     * 页面编辑状态的数据添加
     * @return
     */
    @RequestMapping("amoebaSave")
    @ResponseBody
    public Integer amoebaSave(
            Long id,
            String parameter1,
            String parameter2,
            BigDecimal parameter3,
            BigDecimal parameter4,
            BigDecimal parameter5,
            String type
    ) {
        Integer offRwo = amoebaService.amoebaSave(id,parameter1,parameter2,parameter3,parameter4,parameter5,type);
        return offRwo;
    }

    /**
     * 页面编辑状态的数据添加
     * @return
     */
    @RequestMapping("amoebaUpdateByFZ")
    @ResponseBody
    public Integer amoebaUpdateByFZ(
            Long id,
            BigDecimal parameter5,
            BigDecimal parameter6,
            BigDecimal parameter7,
            BigDecimal parameter8,
            String type
    ) {
        Integer offRwo = amoebaService.amoebaUpdateByFZ(id,parameter5,parameter6,parameter7,parameter8,type);
        return offRwo;
    }

}
